/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvPointage;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTravail;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvPointageJpaController implements Serializable {

    public CvPointageJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvPointage cvPointage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTravail travailleurId = cvPointage.getTravailleurId();
            if (travailleurId != null) {
                travailleurId = em.getReference(travailleurId.getClass(), travailleurId.getCvTravailleurId());
                cvPointage.setTravailleurId(travailleurId);
            }
            em.persist(cvPointage);
            if (travailleurId != null) {
                travailleurId.getCvPointageList().add(cvPointage);
                travailleurId = em.merge(travailleurId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvPointage cvPointage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvPointage persistentCvPointage = em.find(CvPointage.class, cvPointage.getId());
            CvTravail travailleurIdOld = persistentCvPointage.getTravailleurId();
            CvTravail travailleurIdNew = cvPointage.getTravailleurId();
            if (travailleurIdNew != null) {
                travailleurIdNew = em.getReference(travailleurIdNew.getClass(), travailleurIdNew.getCvTravailleurId());
                cvPointage.setTravailleurId(travailleurIdNew);
            }
            cvPointage = em.merge(cvPointage);
            if (travailleurIdOld != null && !travailleurIdOld.equals(travailleurIdNew)) {
                travailleurIdOld.getCvPointageList().remove(cvPointage);
                travailleurIdOld = em.merge(travailleurIdOld);
            }
            if (travailleurIdNew != null && !travailleurIdNew.equals(travailleurIdOld)) {
                travailleurIdNew.getCvPointageList().add(cvPointage);
                travailleurIdNew = em.merge(travailleurIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvPointage.getId();
                if (findCvPointage(id) == null) {
                    throw new NonexistentEntityException("The cvPointage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvPointage cvPointage;
            try {
                cvPointage = em.getReference(CvPointage.class, id);
                cvPointage.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvPointage with id " + id + " no longer exists.", enfe);
            }
            CvTravail travailleurId = cvPointage.getTravailleurId();
            if (travailleurId != null) {
                travailleurId.getCvPointageList().remove(cvPointage);
                travailleurId = em.merge(travailleurId);
            }
            em.remove(cvPointage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvPointage> findCvPointageEntities() {
        return findCvPointageEntities(true, -1, -1);
    }

    public List<CvPointage> findCvPointageEntities(int maxResults, int firstResult) {
        return findCvPointageEntities(false, maxResults, firstResult);
    }

    private List<CvPointage> findCvPointageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvPointage.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvPointage findCvPointage(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvPointage.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvPointageCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvPointage> rt = cq.from(CvPointage.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
